/*
 * HibernateUtil.java
 * 
 * Copyright (c) 2011 Administrator. 
 * 
 * This file is part of RMS.
 * 
 * RMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RMS.  If not, see <http ://www.gnu.org/licenses/>.
 */
package com.itrustsolution.rms.util;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author Administrator
 */
public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();
    public static final ThreadLocal session = new ThreadLocal();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable e) {
            System.err.println("Error initialize session factory: " + e);
            throw new ExceptionInInitializerError(e);
        }

    }

    public static Session getCurrentSession() throws HibernateException {
        Session s = (Session) session.get();
        // Open a new Session, if this thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            // Store it in the ThreadLocal variable
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        if (s != null) {
            s.close();
        }
        session.set(null);
    }

    /**
     * @return the sessionFactory
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static String saveOrUpdate(Object obj) {
        String result = "";
        try {
            Session s = getCurrentSession();
            s.beginTransaction();
            s.saveOrUpdate(obj);
            s.getTransaction().commit();
            result = "success";
        } catch (HibernateException e) {
            result = "failed";
        }
        return result;
    }
    
    public static Object getEntityById(Class className, Integer id)
    {
        Object obj = null;
        try {
            Session s = getCurrentSession();
            obj = s.get(className, id);
        } catch (HibernateException e) {
        }
        return obj;
    }
    
    public static Object createQuery(String queryString)
    {
        Object obj = null;
        try {
            Session s = getCurrentSession();
            Query query = s.createQuery(queryString);
            obj = query.list();
        } catch (HibernateException e) {
        }
        return obj;
    }
}
